<?php

//require_once(sfConfig::get('sf_plugins_dir').'/sfDoctrineGuardPlugin/lib/user/sfGuardSecurityUser.class.php');
    


class myUser extends sfBasicSecurityUser{

	function getUserProfile(){
		$code = $this->getAttribute('code');
		return Doctrine::getTable('personne')->findOneBy('code', $code);
	}
	
	function _toString(){
		return $this->getName();
	}
	
	function getUserName(){
		$user = $this->getUserProfile();
		return trim($user->getPrenom().' '.$user->getNom());
	}
	
	function isSuperAdmin(){
		if($this->getAttribute('administrator'))
	  	{
	  		return true;
	  	}	
	  	else
	  	{
	  		return false;
	  	}
	}
	
	public function login($compte, $pwd){
	  $participant = Doctrine::getTable('participant')->findOneBy('email', $compte);
	  if($participant){
      	if($participant->checkPassword($pwd)){
      		$this->setAuthenticated(true);
      		return $participant->login();
      	}
      }
      return false;
	}
	
	public function logout(){
	  $participant = $this->getUserProfile();
	  if($participant){
	  	$participant->logout();
      }
      $this->setAuthenticated(false);
      $this->getAttributeHolder()->clear();
	  $this->shutdown(); 
      
      return false;
	}
	
	function getRoles(){
		$user = $this->getUserProfile();
		return $user->getRoles();//$this->getAttribute('roles');
	}	
	  /**
	   * Returns whether or not the user has the given credential.
	   * J'ai du reecrire cette fonction, sinon, elle ne marchais pas car nous
	   * N'utilsons pas l'objet sfGuardUser
	   *  
	   * @param string $credential The credential name
	   * @param boolean $useAnd Whether or not to use an AND condition
	   * @return boolean
	   */
	  public function hasCredential($credentials, $useAnd = true)
	  {
	  	if($this->getAttribute('administrator'))
	  	{
	  		return true;
	  	}	
	    if (null === $this->credentials)
	    {
	      return false;
	    }
	    if (!is_array($credentials))
	    {
	      return in_array($credentials, $this->credentials);
	    }
	    // now we assume that $credentials is an array
	    $test = false;
	
	    foreach ($credentials as $credential)
	    {
	      // recursively check the credential with a switched AND/OR mode
	      $test = $this->hasCredential($credential, $useAnd ? false : true);
	
	      if ($useAnd)
	      {
	        $test = $test ? false : true;
	      }
	
	      if ($test) // either passed one in OR mode or failed one in AND mode
	      {
	        break; // the matter is settled
	      }
	    }
	
	    if ($useAnd) // in AND mode we succeed if $test is false
	    {
	      $test = $test ? false : true;
	    }
	
	    return $test;
	  }
}
